<template>
  <el-dialog title="生产制作单" v-if="infoOpen" :visible.sync="infoOpen" :modal="false" width="80%" center>
    <div id="printArea" slyle="margin-top: 250px;">
      <el-table v-loading="loading" :data="productList" :header-cell-style="tableHeaderColor" :span-method="arraySpanMethod" align="center">
        <el-table-column label="车间制作件作业单" align="center">
          <el-table-column label="客户名称" align="center">
            <el-table-column label="序号" :index="indexMethod" type="index" width="80" align="center"/>
            <el-table-column label="产品名称" align="center" prop="productName"/>
          </el-table-column>
          <el-table-column :label="proManufactureOrder.clientName" align="center">
            <el-table-column label="规格/图号" align="center" prop="productCode"/>
            <el-table-column label="单位" width="60" align="center" prop="unitOfMeasure"/>
            <el-table-column label="数量" width="60" align="center" prop="quantity"/>
            <el-table-column label="发货日期" width="85" align="center" prop="requestDate"/>
          </el-table-column>
          <el-table-column label="合同号" align="center">
            <el-table-column label="计划工时" width="85" align="center" prop="jhgs"/>
          </el-table-column>
          <el-table-column :label="proManufactureOrder.contractCode" align="center">
            <el-table-column label="增补工时" width="85" align="center" prop="zbgs"/>
            <el-table-column label="实际工时" width="85" align="center"/>
          </el-table-column>
          <el-table-column label="下发日期" align="center">
            <el-table-column label="返工原因" align="center">
              <el-table-column label="设计" width="70" align="center" prop="sj"/>
              <el-table-column label="外协" width="70" align="center" prop="wx"/>
            </el-table-column>
          </el-table-column>
          <el-table-column :label="proManufactureOrder.moTime" align="center">
            <el-table-column label="备注" align="center">
              <el-table-column label="采购" width="70" align="center"/>
              <el-table-column label="其他" width="70" align="center"/>
            </el-table-column>
            <el-table-column label="" align="center">
              <el-table-column label="" width="60" align="center"/>
            </el-table-column>
          </el-table-column>
        </el-table-column>
      </el-table>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="print">打 印</el-button>
      <el-button @click="infoOpen = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getManufactureorder } from "@/api/mes/pro/manufactureorder";
import printJS from "print-js";

export default {
  name: "WorkSheet",
  props: {
    workorderCode: null,
  },
  data() {
    return {
      infoOpen: false,
      moId: undefined,
      tabIndex: 0,
      proManufactureOrder: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 过程检验单行表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
    };
  },

  methods: {
    print() {
      printJS({
        type: 'html',
        printable: 'printArea',
        // 写入自定义样式
        headerStyle: 'text-align:center;width:100%;margin-top:80px;font-weight: 300;',
        style: `
          @page {
            size: A4 landscape;
            margin: 2.5cm 1cm 2cm 1cm;
          }

          .noprint {
            display: none !important;
          }
        `,
        scanStyles: true,
        // 继承样式
        targetStyles: ['*'],
        maxWidth: 1920
      });
    },
    init(moId) {
      this.loading = true;
      this.productList = [];
      getManufactureorder(moId).then(response => {
        this.loading = false;
        this.infoOpen = true;
        this.proManufactureOrder = response.data
        this.productList.push(this.proManufactureOrder);

        this.aaa = {
          index: '下单人',
          productName: this.proManufactureOrder.lister,
          productSpc: '接收人',
          unitOfMeasure: null,
          quantity: '质检员',
          zbgs: null,
          jhgs: '责任人',
          wx: null,
        }
        this.productList.push(this.aaa);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    indexMethod(index) {
      const lastRowIndex = this.productList.length - 1;
      if (index == lastRowIndex) {
        return '下单人';
      }
      return index + 1;
    },
    tableHeaderColor({row, column, rowIndex, columnIndex}) {
      if (column.label === '返工原因') {
        // if(columnIndex === 1) {
        this.$nextTick(() => {
          if (document.getElementsByClassName(column.id).length !== 0) {
            document
                .getElementsByClassName(column.id)[ 0 ]
                .setAttribute("colspan", 4);
            return false;
          }
        });
        return column;
      } else if (column.label === '备注') {
        this.$nextTick(() => {
          if (document.getElementsByClassName(column.id).length !== 0) {
            document
                .getElementsByClassName(column.id)[ 0 ]
                .setAttribute("rowSpan", 2);
            return false;
          }
        });
        return column;
      }
    },
    arraySpanMethod({row, column, rowIndex, columnIndex}) {
      const lastRowIndex = this.productList.length - 1;
      if (rowIndex === lastRowIndex) {
        if (column.label == '单位') {
          return [1, 3];
        } else if (column.label == '发货日期') {
          return [1, 2];
        } else if (column.label == '增补工时') {
          return [1, 4];
        }
      }
    },
  }
};
</script>
